<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    let list = [{
        id: "1",
        path: '/home',
        name: 'default',
        label: "首页",
        icon: "HomeFilled",
        meta: {
            requireAuth: true, // 当前页面是否需要登录
            role: ["super", "admin", "user"], // 哪些用户可以访问当前页面 
        },
        component: "/Home/Layout/index",
    }, {
        id: "2",
        path: "/admin",
        name: "admin",
        label: "人员管理",
        meta: { requireAuth: true, role: ["super"] },
        icon: "User",
        redirect: "/admin/list",
        // component: AdminView,  (!!! 如果此处设置component => children中的内容就是三级视图,如果不设置children中的内容就是二级视图)

        // 三级视图
        children: [{
            id: "6",
            path: '/admin/list',   // 在二级视图中展示
            name: 'admin-list',
            label: "人员列表",
            icon: "",
            meta: { requireAuth: true, role: ["super"] },
            component: "/Home/Admin/AdminList/index",
        }, {
            id: "7",
            path: '/admin/add',
            name: 'admin-add',
            label: "人员新增",
            icon: "",
            meta: { requireAuth: true, role: ["super"] },
            component: "/Home/Admin/AdminAdd/index"
        }]
    }, {
        id: "3",
        path: "/product",
        name: "product",
        redirect: "/product/list",
        label: "商品管理",
        icon: "Goods",
        meta: { requireAuth: true, role: ["super", "admin", "user"] },
        children: [{
            id: "8",
            path: "/product/list",
            name: "product-list",
            label: "商品列表",
            icon: "",
            meta: { requireAuth: true, role: ["super", "admin", "user"] },
            component: "/Home/Product/ProductList/index",
        }, {
            id: "9",
            path: "/product/manage",
            name: "product-manage",
            label: "推荐秒杀",
            icon: "",
            meta: { requireAuth: true, role: ["super", "admin"] },
            component: "/Home/Product/ProductManage/index"
        }]
    }, {
        id: "4",
        path: "/banner",
        name: "banner",
        redirect: "/banner/list",
        label: "轮播管理",
        icon: "Wallet",
        meta: { requireAuth: true, role: ["super", "admin", "user"] },
        children: [{
            id: "10",
            path: "/banner/list",
            name: "banner-list",
            label: "轮播列表",
            icon: "",
            meta: { requireAuth: true, role: ["super", "admin"] },
            component: "/Home/Banner/BannerList/index"
        }, {
            id: "11",
            path: "/banner/show",
            name: "banner-show",
            label: "轮播展示",
            icon: "",
            meta: { requireAuth: true, role: ["super", "admin", "user"] },
            component: "/Home/Banner/BannerShow/index"
        }]
    }, {
        id: "5",
        path: "/order",
        name: "order",
        redirect: "/order/list",
        label: "订单管理",
        icon: "Menu",
        meta: { requireAuth: true, role: ["super", "admin", "user"] },
        children: [{
            id: "12",
            path: "/order/list",
            name: "order-list",
            label: "订单列表",
            icon: "",
            meta: { requireAuth: true, role: ["super", "admin"] },
            component: "/Home/Banner/BannerList/index"
        }, {
            id: "13",
            path: "/order/manage",
            name: "order-manage",
            label: "订单管理",
            icon: "",
            meta: { requireAuth: true, role: ["super", "admin"] },
            component: "/Home/Banner/BannerShow/index"
        }]
    }]


    function flatTree(treeList, pid = null) {
        treeList = JSON.parse(JSON.stringify(treeList));
        let tree = [];
        for (let i = 0; i < treeList.length; i++) {
            let item = treeList[i];
            item.pid = pid;
            tree.push(item);
            if (item.children) {
                let childTree = flatTree(item.children, item.id);
                if (childTree.length) {
                    tree.push(...childTree);
                }
                delete item.children;// 在children中数据拉平之后, 变相删除children 
            }
        }
        tree.sort((a, b) => a.id - b.id);
        return tree;
    }


    let treeList = flatTree(list);
    console.log(treeList);


    document.write(JSON.stringify(treeList));
</script>

</html>